/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mwt.xml.xdbforms.dblayer.metadata.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import mwt.xml.xdbforms.dblayer.metadata.TableMetaData;

/**
 * Wrapper classe per in l'interfaccia ResultSetMetaData
 * Progetto Master Web Technology
 * @author Gianfranco Murador, Cristian Castiglia, Matteo Ferri
 */
public class JDBCTableMetaDataDS extends TableMetaData{


    private ResultSet resultSet = null;
    private ResultSetMetaData metaData = null;
    Connection c = null;
    Statement s = null;


    public JDBCTableMetaDataDS() {
    }

    public JDBCTableMetaDataDS(String table, DataSource ds) {

        String sql = "SELECT * FROM " + table+ " LIMIT 0,0";
        try {
            c = ds.getConnection();
            s = c.createStatement();
            resultSet =  s.executeQuery(sql);
            metaData = resultSet.getMetaData();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
        }finally{

            /*
            if(c != null){
                try {
                    c.close();
                } catch (SQLException ex) {
                    Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
                }
            }*/


            if( s!= null){
                try {
                    s.close();
                } catch (SQLException ex) {
                    Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

    }

    public int getColumnCount() throws SQLException {

        return metaData.getColumnCount();
    }

    public boolean isAutoIncrement(int column) throws SQLException {

        return metaData.isAutoIncrement(column);
    }

    public boolean isCaseSensitive(int column) throws SQLException {

        return metaData.isCaseSensitive(column);
    }

    public boolean isSearchable(int column) throws SQLException {
        return metaData.isSearchable(column);
    }

    public boolean isCurrency(int column) throws SQLException {
        return metaData.isCurrency(column);
    }

    public int isNullable(int column) throws SQLException {
        return metaData.isNullable(column);
    }

    public boolean isSigned(int column) throws SQLException {
        return metaData.isSigned(column);
    }

    public int getColumnDisplaySize(int column) throws SQLException {
        return metaData.getColumnDisplaySize(column);
    }

    public String getColumnLabel(int column) throws SQLException {
        return metaData.getColumnLabel(column);
    }

    public String getColumnName(int column) throws SQLException {
        return metaData.getColumnName(column);
    }

    public String getSchemaName(int column) throws SQLException {
        return metaData.getSchemaName(column);
    }

    public int getPrecision(int column) throws SQLException {
        return metaData.getPrecision(column);
    }

    public int getScale(int column) throws SQLException {
        return metaData.getScale(column);
    }

    public String getTableName(int column) throws SQLException {
        return metaData.getTableName(column);
    }

    public String getCatalogName(int column) throws SQLException {
        return metaData.getCatalogName(column);
    }

    public int getColumnType(int column) throws SQLException {
        return metaData.getColumnType(column);
    }

    public String getColumnTypeName(int column) throws SQLException {
        return metaData.getColumnTypeName(column);
    }

    public boolean isReadOnly(int column) throws SQLException {
        return metaData.isReadOnly(column);
    }

    public boolean isWritable(int column) throws SQLException {
        return metaData.isWritable(column);
    }

    public boolean isDefinitelyWritable(int column) throws SQLException {
        return metaData.isDefinitelyWritable(column);

    }

    public String getColumnClassName(int column) throws SQLException {

        return metaData.getColumnClassName(column);

    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return metaData.unwrap(iface);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return metaData.isWrapperFor(iface);
    }

    @Override
    public void close() {
        if(c != null)
        {
            try {
                c.close();
            } catch (SQLException ex) {
                Logger.getLogger(JDBCTableMetaDataDS.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

}
